﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3D
{
    class Tree : GameObject3D
    {
        VertexPositionTexture[] verts;

        VertexBuffer vertexBuffer;
        BasicEffect effect;
        Vector3 position;
        public  Vector2 distanceCamera;

        public Tree(Camera camera, GraphicsDevice graphics, Vector3 position, float size, Texture2D txt)
        {
            verts = new VertexPositionTexture[4];
            this.position = position;

            verts[0] = new VertexPositionTexture(new Vector3(size , size, 0), new Vector2(0, 0));
            verts[1] = new VertexPositionTexture(new Vector3(-size , size , 0), new Vector2(1, 0));
            verts[2] = new VertexPositionTexture(new Vector3(size , -size ,0), new Vector2(0, 1));
            verts[3] = new VertexPositionTexture(new Vector3(-size, -size , 0), new Vector2(1, 1));

            this.position = position;
            vertexBuffer = new VertexBuffer(graphics, typeof(VertexPositionTexture), verts.Length, BufferUsage.None);
            vertexBuffer.SetData<VertexPositionTexture>(verts);

            effect = new BasicEffect(graphics);
            effect.Texture = txt;
            effect.TextureEnabled = true;

        }

        public override void Update(Camera camera)
        {
            effect.View = camera.view;
            effect.Projection = camera.projection;
            effect.World = Matrix.Identity;
            effect.World *= Matrix.CreateRotationY(camera.rotation);
            effect.World *= Matrix.CreateTranslation(position);
            distanceCamera = new Vector2(camera.position.X - position.X,camera.position.Z - position.Z);

            base.Update(camera);
        }
        public override void Draw(GraphicsDevice graphics)
        {
            graphics.BlendState = BlendState.AlphaBlend;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                graphics.SetVertexBuffer(vertexBuffer);
                graphics.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, verts, 0, 2);
            }
            base.Draw(graphics);
        }
    }
}
